package com.github.chirspan.xaas.gateway.component;

import cn.hutool.core.util.URLUtil;
import com.github.chirspan.xaas.core.constant.XaasConstants;
import com.github.chirspan.xaas.gateway.interceptor.OAuthFeignRequestInterceptor;
import com.github.chirspan.xaas.gateway.service.OAuthClient;
import feign.Client;
import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.slf4j.Slf4jLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author chenpan
 * @date 2020/5/26 18:28
 */
@Component
public class FeignClientFactory {

    @Autowired
    Client client;

    @Autowired
    Contract feignContract;

    @Autowired
    Encoder feignEncoder;

    @Autowired
    Decoder feignDecoder;

    public OAuthClient oAuthClient(String token) {
        return Feign.builder()
                .client(client)
                .logger(new Slf4jLogger(OAuthClient.class))
                .encoder(feignEncoder)
                .decoder(feignDecoder)
                .contract(feignContract)
                .requestInterceptor(new OAuthFeignRequestInterceptor(token))
                .logLevel(Logger.Level.FULL)
                .target(OAuthClient.class, URLUtil.normalize(XaasConstants.API_OAUTH_PROVIDER));
    }
}
